package com.android.fragmentdemo.defineviews
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class SnapHelperItemDecoration(rightSpace: Int, lastItemNeed: Boolean = false) : RecyclerView.ItemDecoration() {

    private val mRightSpace = rightSpace
    private val mLastItemNeed = lastItemNeed

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        val childIndex = parent.getChildAdapterPosition(view)
        val itemCount = parent.adapter?.itemCount ?: 0
        if (childIndex != itemCount - 1) {
            outRect.right = mRightSpace
        } else {
            outRect.bottom = if (mLastItemNeed) {
                mRightSpace
            } else {
                0
            }
        }
    }
}